//
//  replayView.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/4/6.
//  Copyright © 2016年 zz. All rights reserved.
//

#define LikeLabelLineHeight 1.1f

#define LikeIconLeftMargin 8
#define LikeIconTopMargin 14
#define LikeIconSize 15

#define LikeLabelIconSpace 5
#define LikeLabelRightMargin 10

#define CommentLabelLineHeight 1.2f
#define HighLightTextColor [UIColor colorWithRed:56/255.0f green:112/255.0f blue:193/255.0f alpha:1.0]
#define HighLightTextColorNor [UIColor colorWithHexString:@"#1ebfff"]


#import "replayView.h"

#import "MLLinkClickLabel.h"
#import "MLClickColorLinkLabel.h"

#import "DFBaseLineItem.h"
#import "DFLineCommentItem.h"
#import "MLLabel+Size.h"


@interface replayView()<MLLinkClickLabelDelegate>

@property(nonatomic,strong) UIImageView *likeCmtBg;  //背景图片

@property(nonatomic,strong) UIImageView *likeIconView;
@property(nonatomic,strong) MLLinkLabel *likeLabel;
@property (strong, nonatomic) UIView *divider;

@property (strong, nonatomic) NSMutableArray *commentLabels;

@end

@implementation replayView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        _commentLabels = [NSMutableArray array];
        [self initView];
    }
    
    return self;
}

-(void)initView{
   
    CGFloat x,y,width,height;
    
    if (_likeCmtBg == nil) {
        
        x = 0;
        y = 0;
        width = self.frame.size.width;
        height = self.frame.size.height;
        
        _likeCmtBg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        UIImage *image = [UIImage imageNamed:@"LikeCmtBg"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 30, 10, 10) resizingMode:UIImageResizingModeStretch];
        _likeCmtBg.image = image;
        
        [self addSubview:_likeCmtBg];
    }
    
    if (_likeIconView == nil) {
        _likeIconView = [[UIImageView alloc] init];
        
        x = 10;
        y = 13.5;
        width = 15;
        height = 15;
        
        _likeIconView.frame = CGRectMake(x, y, width, height);
        _likeIconView.image = [UIImage imageNamed:@"zan_nor"];
        [self addSubview:_likeIconView];
    }
    
    if (_likeLabel == nil) {
        
        _likeLabel =[[MLLinkLabel alloc] initWithFrame:CGRectZero];
        _likeLabel.font = [UIFont systemFontOfSize:14];
        _likeLabel.numberOfLines = 0;
        _likeLabel.adjustsFontSizeToFitWidth = NO;
        _likeLabel.textInsets = UIEdgeInsetsZero;
        
        _likeLabel.dataDetectorTypes = MLDataDetectorTypeAll;
        _likeLabel.allowLineBreakInsideLinks = NO;
        _likeLabel.linkTextAttributes = nil;
        _likeLabel.activeLinkTextAttributes = nil;
        _likeLabel.lineHeightMultiple = 1.1f;
//        _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName:HighLightTextColor};
        _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName:HighLightTextColorNor};
        
        __block replayView *likeCommentView = self;
        
        [_likeLabel setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
            
            if (self->_delegate != nil && [self->_delegate respondsToSelector:@selector(onclikerUser:)]) {
                
                NSUInteger userid = [link.linkValue integerValue];
                [likeCommentView.delegate onclikerUser:userid];
            }
        }];
        
        [self addSubview:_likeLabel];
    }
    
    if (_divider == nil) {
        _divider = [[UIView alloc] initWithFrame:CGRectZero];
        _divider.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [self addSubview:_divider];
    }

}

-(void)layoutSubviews{
    
    CGFloat x,y,width,height;
    x = 0;
    y = 0;
    width = self.frame.size.width;
    height = self.frame.size.height;
    
    _likeCmtBg.frame = CGRectMake(x, y, width, height);
}

+(CGFloat)getHeight:(DFBaseLineItem *)item maxWidth:(CGFloat)maxWidth{
    
    CGFloat height = 6;  //这个是背景上面箭头的高度
    
    if (item.praiseuser.count > 0) {
        
        CGFloat width = maxWidth -  LikeIconLeftMargin - LikeIconSize - LikeLabelIconSpace - LikeLabelRightMargin;
        
        CGSize textSize = [MLLinkLabel getViewSize:item.likesStr maxWidth:width font:[UIFont systemFontOfSize:14] lineHeight:LikeLabelLineHeight lines:0];
        height+= textSize.height + 13;  //13是测量label 距上和距下距离和
    }
    
    if (item.replylist.count > 0) {
        
        CGFloat width = maxWidth - 3*2;
        
        height += 0.5;   //分割线的高度
        height += 2;  //评论区第一个label距上面线的距离
        
        NSMutableArray *commentStrArray = item.commentStrArray;
        
        for (NSMutableAttributedString *str in commentStrArray) {

            CGSize textSize = [MLLinkLabel getViewSize:str maxWidth:width font:[UIFont systemFontOfSize:14] lineHeight:1.2f lines:0];

            height+= textSize.height;
        }
        
        height += 7.5;  //最后一个label距底部距离
        
    }
    
    
//    height+=6;

    return height;

}

-(void)updateWithItem:(DFBaseLineItem *)item{
    
    CGFloat x, y, width, height;
    
    _divider.hidden = YES;
    
    if (item.praiseuser.count > 0) {  //有赞
        _likeLabel.hidden = NO;
        _likeIconView.hidden = NO;
    
    
        x = CGRectGetMaxX(_likeIconView.frame)+LikeLabelRightMargin;
        y = 12;
        width = self.frame.size.width - x - 10;
        
        NSMutableAttributedString *likesStr = item.likesStr;
        _likeLabel.attributedText = likesStr;
        
        [_likeLabel sizeToFit];
        
        CGSize textSize = [MLLinkLabel getViewSize:likesStr maxWidth:width font:[UIFont systemFontOfSize:14] lineHeight:LikeLabelLineHeight lines:0];
        
        _likeLabel.frame = CGRectMake(x, y, width, textSize.height);
    }else{   //没有赞
        
        _likeLabel.hidden = YES;
        _likeIconView.hidden = YES;

    }
    
    if (item.replylist.count > 0) {
        
        if (item.praiseuser.count > 0) {  //有赞有评论
            
            y = CGRectGetMaxY(_likeLabel.frame) + 7.5;
            _divider.hidden = NO;
            _divider.frame = CGRectMake(0, y, self.frame.size.width, 0.5);
        }
        
        CGFloat sumHeight = 9;
        if (item.praiseuser.count > 0) {
            sumHeight = CGRectGetMaxY(self.divider.frame) + 2.5;
        }
        
        NSUInteger labelCount = _commentLabels.count;
        
        for (int i=0; i<labelCount; i++) {
            MLLinkClickLabel *label = [_commentLabels objectAtIndex:i];
            label.attributedText = nil;
            label.frame = CGRectZero;
            label.hidden = !(i<item.replylist.count);
        }
        
        for (int i = 0; i<item.commentStrArray.count; i++) {
            
            MLLinkClickLabel *label;
            
            if ( labelCount > 0 && i < labelCount) {
                label = [_commentLabels objectAtIndex:i];
            }else{
                label = [self createLinkLabel];
                [_commentLabels addObject:label];
                [self addSubview:label];
            }
            
            NSDictionary *dic = [item.replylist objectAtIndex:i];
            
            label.hidden = NO;
            
            NSAttributedString *str = [item.commentStrArray objectAtIndex:i];
            label.attributedText = str;
            
            label.uniqueId = [[dic objectForKey:@"userid"] integerValue];
            label.userName = [dic objectForKey:@"username"];
            label.userid = [[dic objectForKey:@"userid"] stringValue];
            label.toUserName = [dic objectForKey:@"tousername"];
            
            label.replayId = [[dic objectForKey:@"replyid"] integerValue];
//            label.backgroundColor = [UIColor greenColor];
            [label sizeToFit];
            
            width = self.frame.size.width - 2 * 3;
            
            CGSize size = [MLLabel getViewSize:label.attributedText maxWidth:width font:[UIFont systemFontOfSize:14] lineHeight:CommentLabelLineHeight lines:0];
            
            x = 10;
            y = sumHeight;
            height = size.height;
            sumHeight+=height;
            
            
            
            label.frame = CGRectMake(x, y, width, height); //让label距上下有一个距离
            
        }
    }else{
        
        for (int i=0; i<_commentLabels.count; i++) {
            MLLinkClickLabel *label = [_commentLabels objectAtIndex:i];
            label.attributedText = nil;
            label.frame = CGRectZero;
            label.hidden = YES;
        }

    }
}

-(MLClickColorLinkLabel *)createLinkLabel{
    
    MLClickColorLinkLabel *lable = [[MLClickColorLinkLabel alloc] initWithFrame:CGRectZero];
    lable.clickDelegate = self;
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [lable addGestureRecognizer:longPress];
    
    lable.tag = 100;
    lable.font = [UIFont systemFontOfSize:14];
    lable.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    lable.numberOfLines = 0;
    lable.adjustsFontSizeToFitWidth = NO;
    lable.textInsets = UIEdgeInsetsZero;
    
    lable.dataDetectorTypes = MLDataDetectorTypeAll;
    lable.allowLineBreakInsideLinks = NO;
    lable.linkTextAttributes = nil;
    lable.activeLinkTextAttributes = nil;
    lable.lineHeightMultiple = CommentLabelLineHeight;
    lable.linkTextAttributes = @{NSForegroundColorAttributeName: HighLightTextColorNor};
    
    __block replayView *likeCommentView = self;
    
    [lable setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
        
        if (self->_delegate != nil && [self->_delegate respondsToSelector:@selector(onclikerUser:)]) {
            
            NSUInteger userId = [link.linkValue integerValue];
            [likeCommentView.delegate onclikerUser:userId];
        }
    }];
        
    return lable;
}


-(void)longPress:(UITapGestureRecognizer*)recognizer
{
    
}

-(void)onClickOutsideLink:(NSString *)userName andUserid:(NSString *)userid andReplayid:(NSInteger)replayid
{

    if (_delegate && [_delegate respondsToSelector:@selector(onclikerComment:andUserid:andTag:andReplayid:)]) {
        [_delegate onclikerComment:userName andUserid:userid andTag:(int)self.tag andReplayid:replayid];
    }
}

@end
